/* gpprtrpa.i - Set printer for output                                  */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/* $Revision: 1.14 $                                                         */
/*V8:ConvertMode=NoConvert                                              */
/* Revision: 8.0      Last Modified: 02/18/95   By: gui                 */
/* Revision: 8.4      Last Modified: 04/12/95   By: jpm                 */
/* Revision: 8.4      Last Modified: 04/25/95   By: srk                 */
/* Revision: 8.5      Last Modified: 10/16/95   By: jpm  *J08V*/
/* Revision: 8.5      Last Modified: 02/26/96   By: rkc  *G1D6*         */
/* Revision: 8.5      Last Modified: 03/11/96   By: rkc  *G1MR*/
/* REVISION: 8.7      LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane */
/* REVISION: 9.1      LAST MODIFIED: 09/21/99   BY: *N03C* Kieran O Dea */
/* REVISION: 9.1      LAST MODIFIED: 09/05/00   BY: *N0RF* Mark Brown   */
/* REVISION: 9.1      LAST MODIFIED: 09/12/00   BY: *L12Y* Falguni D.   */
/* $Revision: 1.14 $    BY: Hualin Zhong  DATE: 09/06/01  ECO: *N11T*          */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/* DEFINE VARIABLES */
define variable printsetup as logical no-undo.
define variable savestate as logical no-undo.
define variable l_destinationType as integer no-undo.
define variable l_returnstatus as logical no-undo.
define variable l_textfile as character no-undo.
/*======================================================================*/

/* INITIALISATION */

flag-report-exit = false.
printWidth = {2}.

/*======================================================================*/
/* FIND OUTPUT DESTINATION DEFINITION */
find prd_det where prd_dev = dev no-lock no-error.

/*======================================================================*/
/* SET DESTINATION PATHNAME */

if file_it then do:

   if index(file_name,".") = 0 then
      path = file_name + ".prn".
   else
      path = file_name.
   dev = path.
end. /* if file_it */
else
if available prd_det then
   path = if (prd_path <> "") then prd_path else prd_dev.
else
   path = dev.

/*===========================================================================*/
/* VALIDATE OUTPUT DESTINATION */

if available prd_det then do:

   &IF "{&WINDOW-SYSTEM}" = "TTY" &THEN
   if path = "window" then do:
      /* Output to WINDOW is only available with MFG/PRO for Windows */
      {pxmsg.i &MSGNUM=8005 &ERRORLEVEL=3}
      leave.
   end.

   else if prd_dest_type = 2 then do: /*WINDOWS PRINTER*/
      /* Output to WinPrint is only available with MFG/PRO for Windows */
      {pxmsg.i &MSGNUM=3372 &ERRORLEVEL=3}
      leave.
   end.

   else if path = "terminal" or path = "/dev/tty" or path = "tt:" then do:
      /* TERMINAL not supported for browses and object programs */
      {pxmsg.i &MSGNUM=8006 &ERRORLEVEL=3}
      leave.
   end.
   &ENDIF

   if prd_scroll_only and "{3}" <> "" then do:
      /*Maximum Page Limit Printing not allowed with no-page option */
      {pxmsg.i &MSGNUM=89 &ERRORLEVEL=3}
      leave.
   end.
   if prd_path <> "" then path = prd_path.
   scrollonly = prd_scroll_only.
end.
else do:
   if not batchrun then do:
      /* DEVICE ENTERED IS NOT A DEFINED PRINTER. */
      {pxmsg.i &MSGNUM=34 &ERRORLEVEL=2}
   end.
end.

if (path = "terminal" or path = "/dev/tty" or path = "tt:"
   &IF "{&WINDOW-SYSTEM}" <> "TTY" &THEN
   or path = "window"
   &ENDIF
   or scrollonly)
   and batch_id <> "" then do:
   /* Output to terminal not allowed for batch request */
   {pxmsg.i &MSGNUM=66 &ERRORLEVEL=3}
   leave.
end.

if (path = "terminal" or path = "/dev/tty" or path = "tt:"
   or scrollonly) and "{4}" > "" then do:
   /* Terminal not allowed */
   {pxmsg.i &MSGNUM=35 &ERRORLEVEL=3}
   leave.
end.

/*======================================================================*/
assign
   printsetup = false
   savestate = session:system-alert-boxes
   session:system-alert-boxes = false.
do on error undo, leave:

   if batch_id = "" then do:

      /*======================================================================*/
      /* PROCESS OUTPUT TO DESTINATION INTERACTIVELY */

      /*---------------------- INITIALISE PRINT PROPERTIES -------------------*/
      assign
         maxpage = 0
         pagesize = 66
         spooler = no
         scrollonly = no
         l_destinationType = 0. /*SERVER PRINTER*/

      if available prd_det then do:
         assign
            maxpage = prd_max_page
            pagesize = prd_length
            spooler = prd_spooler
            l_destinationType = prd_dest_type.

         if not file_it then
            scrollonly = prd_scroll_only.
      end.

      /* Set spooler to no if not running under unix or scrolling output */
      if path = "printer" or opsys <> "unix" or scrollonly or file_it then
         spooler = no.

      /*--------------------- INITIALISE output file  ---------------------*/

      if scrollonly then do:
         path = mfguser + ".out".
         if search(path) <> ? then do:
            {gpfildel.i &filename=search(path)}
         end.
      end.
      else
      if not available prd_det and path <> "printer" and path <> "terminal"
         &IF "{&WINDOW-SYSTEM}" <> "TTY" &THEN
         and path <> "window"
         &ENDIF
      then do:
         if index(path,".") = 0 then
            path = path + ".prn".
      end.

      /*---------------------- MESSAGE REPORT RUNNING ---------------------*/

      &IF "{&WINDOW-SYSTEM}" = "TTY" &THEN
      /*  The following should be uncommented when printing to terminal is
       * supported for character browses and object maint. programs. */

      /* if path <> "terminal" and path <> "/dev/tty" and
       * path <> "tt:" then do: */
      if opsys = "unix" then do:
         /*Report now running. Press control-C to cancel*/
         {pxmsg.i &MSGNUM=5019 &ERRORLEVEL=1}
      end.
      else
      if opsys = "msdos" or opsys = "win32" then do:
         /*Report now running. Press control-BREAK ...*/
         {pxmsg.i &MSGNUM=5020 &ERRORLEVEL=1}
      end.
      else
      if opsys = "vms" then do:
         /*Report now running. Press control-Y or STOP ...*/
         {pxmsg.i &MSGNUM=5021 &ERRORLEVEL=1}
      end.
      /* end. */
      &ENDIF

      /*--------- SEND INITIALISATION COMMAND TO OUTPUT DESTINATION --------*/

      if available prd_det and prd_init_pro <> "" and not file_it then do:
         if opsys = "unix" then
            unix silent value(prd_init_pro).
         else
         if opsys = "msdos" then
            dos silent value(prd_init_pro).
         else
         if opsys = "vms" then
           vms silent value(prd_init_pro).
      end.

     /*---------------------DETERMINE PAGE LENGTH-----------------------*/

      if "{3}" = "nopage" then
         printlength = 0. /* OUTPUT WILL NOT BE PAGED */
      else /* PAGE SIZE OTHER THAN 0 MEANS THAT OUTPUT WILL BE PAGED */
         printlength = pagesize - 6.

     /*-----------------------------------------------------------------*/

      pause 0 before-hide.
      save-window = ?.

      /* GUI only */
      &IF "{&WINDOW-SYSTEM}" <> "TTY" &THEN
      save-proc-window = ?.

      /*--------------------- SET OUTPUT DESTINATION --------------------*/

      if path = "window" then do:
         run p-report-cancel-button in tools-hdl.
         output {4} to value("report.rpt") /*fdn* paged */.
         report-to-window = true.
      end.
      else
      if l_destinationType = 2 then do: /*WINDOWS PRINTER*/
         {gprunp.i "gpwinprt" "p" "selectPrinter"
            "(input batchrun, output l_returnstatus)"}
         if l_returnstatus = true then do:
            l_textfile = "winprint" + mfguser + ".dat".
            output {4} to value(l_textfile)
               page-size value(printlength).
         end.
         else /*EXIT CURRENT .P PROGRAM */
            return.
      end.
      /*SET OUTPUT DESTINATION FOR EMAIL*/
      else
      if l_destinationType = 1 then do:
         {gprunp.i "gpemail" "p" "getEmailDetails" "(output l_returnstatus)"}
         if l_returnstatus = true then do:
            l_textfile = "email" + mfguser + ".dat".
            output {4} to value(l_textfile) page-size value(printlength).
         end.
         else /*EXIT CURRENT .P PROGRAM */
            return.
      end. /* if l_destinationType = 1 */
      else
      if path = "terminal" then do:
         pause before-hide.
         create window terminal-window
         assign
            TITLE            = dtitle
            THREE-D          = yes
            FONT             = 0
            HEIGHT           = 23.29
            WIDTH            = 100
            MAX-HEIGHT       = 23.29
            MAX-WIDTH        = 100
            VIRTUAL-HEIGHT   = 24
            VIRTUAL-WIDTH    = {2}
            RESIZE           = yes
            SCROLL-BARS      = yes
            STATUS-AREA      = yes
            BGCOLOR          = ?
            FGCOLOR          = ?
            /*  Use message-area so End of Report message is displayed */
            MESSAGE-AREA     = yes
            SENSITIVE        = yes
            PRIVATE-DATA     = getTermLabel("MFG/PRO_PAGED_TERM", 24)
            .
         assign
            save-window = current-window
            current-window = terminal-window.
         if valid-handle(THIS-PROCEDURE:CURRENT-WINDOW) then do:
            assign
               save-proc-window = THIS-PROCEDURE:CURRENT-WINDOW
               THIS-PROCEDURE:CURRENT-WINDOW = terminal-window.
         end.
         apply "ENTRY" to current-window.
         output {4} to terminal paged.
      end. /* if path = "terminal" then do: */

      &ELSE
      /* Character only */
      /* Remove if false... and uncomment when terminal output is
       * supported for browses and object maintenance programs. */

      if false then do:
      /* if path = "terminal" then do:
       *   pause before-hide.
       *   if "{1}" = "printer" then do:
       *      if can-find (_field where _field-name = "_frozen") then do:
       *         {gprun.i ""gpseterm.p""}
       *      end.
       *      output {4} to terminal paged.
       *   end.
       *   else
       *      release prd_det.
       */
      end.
      &ENDIF

      else do: /* GUI and character code if not terminal and not window */

         &IF "{&WINDOW-SYSTEM}" <> "TTY" &THEN
         if not batchrun then
            run p-report-cancel-button in tools-hdl.
         &ENDIF

         /* THE VARIABLE printlength IS SET TO ZERO SO THAT WHEN */
         /* LINES/PAGE SETTING IS SET TO ZERO REPORT OUTPUT WILL */
         /* NOT HAVE PAGE BREAKS.                                */
         if printlength < 0 then
            printlength = 0.

         if spooler then
            output {4} through value(path) page-size value(printlength).
         else
            output {4} to value(path) {5} page-size value(printlength).
      end. /* else do... */

      /*-------------------------------------------------------------------*/

      &IF "{&WINDOW-SYSTEM}" <> "TTY" &THEN
      if path <> "terminal" and path <> "window" then do:
      &ENDIF

      /*------------------ SEND PRINTER CONTROL CODES -------------------*/

      if available prd_det then do:
         if {2} = 132 then
            put {4} control prd_init + prd_st_132.
         else
         if {2} = 80 then
            put {4} control prd_init + prd_st_80.
         else
            put prd_init.
      end.

      /*-----------------------------------------------------------------*/

      &IF "{&WINDOW-SYSTEM}" <> "TTY" &THEN
      end.
      &ENDIF
   end. /* if batch_id = "" */
   else do:

      /*===================================================================*/
      /* PROCESS OUTPUT TO DESTINATION IN BATCH */

      define variable i as integer.
      i = index(dev,".").
      if i > 0 then do:   /* strip off file extension, re-attached at run-time */
         dev = substring(dev, 1, i - 1).
      end.

      {gprun.i ""gpbcdadd.p""}
      if i > 0 then do:   /* make dev valid for next pass at combo-box */
         dev = "".
      end.
      leave.
   end.

   printsetup = true.
end. /* do on error undo, leave */
session:system-alert-boxes = savestate.

if index(dev,".") <> 0 then do:
   dev = "".
end.

if not printsetup then do:
   {mfguidel.i}
   /* If window has a message area, we were able to divert system
    * message to it and now we can display our message */
   if CURRENT-WINDOW:MESSAGE-AREA then do:
      /* Contact Sys. Admin. Unable to output to printer or file */
      {pxmsg.i &MSGNUM=8003 &ERRORLEVEL=4 &MSGARG1=path}
   end.
   {mfreset.i}
   /* Set runok to FALSE so job will register as FAILED in batch */
   runok = false.
   return.
end.
